clear
est clear


cap mat define migrationfigure=J(105,10,.)
		local row=1

local c=1
foreach i in 10 20 40 80 160{
	//Create future return migration panel
	{
	    	clear
			use "$input/full_geoid_pair_panel.dta"
				
			replace migration=migration/(tot_pop/100000)

			
				
			sort from_geoid to_geoid move_year
			
			keep from_geoid to_geoid move_year migration
			
			rename migration migration_into_from_out_to
			
			rename (from_geoid to_geoid) (to_geoid from_geoid)
			
			save "$input/temp1.dta", replace
			
				clear
			use "$input/full_geoid_pair_panel.dta"
				
			replace migration=migration/(tot_pop/100000)

			
				
			sort from_geoid to_geoid move_year
			
			keep from_geoid to_geoid move_year migration
			
			cap drop _merge
			merge 1:1 from_geoid to_geoid move_year using "$input/temp1.dta"
			mvencode migration_into_from_out_to, mv(0) o
			*drop if _merge!=3
			g paired_net_migration=migration_in-migration
			
			drop _merge
			
			save "$input/paired_net_migration.dta", replace
			clear
	}
	
	use "$input/full_geoid_pair_panel.dta"
		
	replace migration=migration/(tot_pop/100000)

	//Make sure only include states that have a storm in at least one county at least once in our sample years
	bysort state_fips: egen max=max(storm)
		drop if max!=1
	
	replace totalap=totalap/1000000
	g highdamage=(totalapp>`i') if totalapp!=.
	
	keep if highdamage==1 & migration>0 & migration!=.
		
	keep from_geoid to_geoid move_year
	duplicates drop from_geoid to_geoid move_year, force
	g high_damage_receiving_county=1
		save "$input/temp.dta", replace
	clear
	
	use "$input/full_geoid_pair_panel.dta"
		replace migration=migration/(tot_pop/100000)
		bysort state_fips: egen max=max(storm)
			drop if max!=1
		
		replace totalap=totalap/1000000
		g highdamage=(totalapp>`i') if totalapp!=.
		merge 1:1 from_geoid to_geoid move_year using "$input/temp.dta", nogen keep(1 3)
	
		cap drop _merge
		merge 1:1 from_geoid to_geoid move_year using "$input/paired_net_migration.dta"
		mvencode highdamage high_damage_receiving_county, mv(0) o
		drop if _merge==2
		bysort from_geoid to_geoid: egen received_storm_migrations=max(high_damage_receiving_county)
		
		drop if received_storm_migrations!=1
		
			
		//Create IHS transformations
		ihstrans( paired_net_migration)
	
		label var high_damage_receiving_county "1(Storm year)"
		
		sort from_geoid to_geoid move_year
		foreach i in 1 2 3 4 5{
				g high_damage_receiving_county_L`i'=high_damage_receiving_county[_n-`i'] if from_geoid==from_geoid[_n-`i'] & to_geoid==to_geoid[_n-`i']
		}	
		foreach var in L1 L2 L3 L4 L5{
			label var high_damage_receiving_county_`var' "1(Storm year), `i'"
		}
		
		
		
		reghdfe ihs_paired_net_migration i.high_damage_receiving_county, absorb(i.from_geoid  i.move_year i.state_fips#c.move_year) vce(cluster state_fips)
			eststo t1c`c'
			cap mat def migrationfigure[`row',1]=_b[1.high_damage_receiving_county]
			cap mat def migrationfigure[`row',2]=_se[1.high_damage_receiving_county]
			cap mat def migrationfigure[`row',3]="no lags, net"
			local row=`row'+1
		reghdfe ihs_paired_net_migration i.high_damage_receiving_county i.high_damage_receiving_county_L*, absorb(i.from_geoid  i.move_year i.state_fips#c.move_year) vce(cluster state_fips)
			eststo t2c`c'
			cap mat def migrationfigure[`row',1]=_b[1.high_damage_receiving_county]
			cap mat def migrationfigure[`row',2]=_se[1.high_damage_receiving_county]
			cap mat def migrationfigure[`row',3]="lags, out"
			local row=`row'+1
			cap mat def migrationfigure[`row',1]=_b[1.high_damage_receiving_county_L1]
			cap mat def migrationfigure[`row',2]=_se[1.high_damage_receiving_county_L1]
			cap mat def migrationfigure[`row',3]="lags, out"
			local row=`row'+1
			cap mat def migrationfigure[`row',1]=_b[1.high_damage_receiving_county_L2]
			cap mat def migrationfigure[`row',2]=_se[1.high_damage_receiving_county_L2]
			cap mat def migrationfigure[`row',3]="lags, out"
			local row=`row'+1
			cap mat def migrationfigure[`row',1]=_b[1.high_damage_receiving_county_L3]
			cap mat def migrationfigure[`row',2]=_se[1.high_damage_receiving_county_L3]
			cap mat def migrationfigure[`row',3]="lags, out"
			local row=`row'+1
			cap mat def migrationfigure[`row',1]=_b[1.high_damage_receiving_county_L4]
			cap mat def migrationfigure[`row',2]=_se[1.high_damage_receiving_county_L4]
			cap mat def migrationfigure[`row',3]="lags, out"
			local row=`row'+1
			cap mat def migrationfigure[`row',1]=_b[1.high_damage_receiving_county_L5]
			cap mat def migrationfigure[`row',2]=_se[1.high_damage_receiving_county_L5]
			cap mat def migrationfigure[`row',3]="lags, out"
			local row=`row'+1	
			
			lincom 1.high_damage_receiving_county+1.high_damage_receiving_county_L1+1.high_damage_receiving_county_L2+1.high_damage_receiving_county_L3+1.high_damage_receiving_county_L4+1.high_damage_receiving_county_L5
				local beta`c'1=trim("`: display %10.3f r(estimate)'")
				local se`c'1=trim("`: display %10.3f r(se)'")	
				cap mat def migrationfigure[`row',1]=`beta`c'1'
				cap mat def migrationfigure[`row',2]=`se`c'1'
				cap mat def migrationfigure[`row',3]="lags, out"
				local row=`row'+1	
			
			local c=`c'+1
	clear	
	
}
				
		
		
		
	file open 	t 		using "$tables/table_si9.tex", replace write
	file write t	"\begin{table}[htbp]\footnotesize \centering" _n "\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}" _n ///
						"\caption{Return migration after high damage storms}\label{tab: total migration effects high damage return}" _n  ///
						"\begin{tabular*}{1\textwidth}{@{\extracolsep{\fill}}l*{10}{c}}" _n "\midrule" _n ///
						"&$>10MM$&$>20MM$&$>40MM$&$>80MM$&$>160MM$&\\" _n ///
						"\midrule" _n 
	file close 	t
	
				esttab t1c* using "$tables/table_si9.tex", l keep(1.high_damage_receiving_county) ///
				s(N, l("N") f(%11.0fc) lay(@)) $opts 
				
	file open 	t 		using "$tables/table_si9.tex", append write
	file write t "\\" ///
					"\midrule																			" ///
					"Fixed Effects:									&		&		&		&		&					\\" ///
					"\hspace{3mm}County								&Yes	&Yes	&Yes	&Yes	&Yes				\\ " ///
					"\hspace{3mm}Year								&Yes	&Yes	&Yes	&Yes	&Yes				\\ " ///
					"\hspace{3mm}State\$\times\$Year				&Yes	&Yes	&Yes	&Yes	&Yes				\\ " ///
							"\midrule" _n "\end{tabular*}" _n ///
							"\begin{tabular*}{1\textwidth}{p{6in}}" _n ///
							"\footnotesize \textsc{Notes:} All columns report the results of a fixed effects specification with the IHS transformation of the number of return-migrants to a county in the five years following a high damage storm as the outcome. We only include return migration from counties that received migrants from the storm-impacted county in the year of the storm. Counties are defined as exposed to a storm if at least one hurricane resulted in a flood warning during the year or the county experienced wind speeds of at least 21 m/s - the speed at which structural damage begins to appear - during a hurricane in the year and if the damage payments from FEMA in response to the storm exceeded the totals indicated in the column headings. \$^{*}\$p=0.1, \$^{**}\$p=0.05,\$^{***}\$p=0.01. Compiled `c(current_date)'." ///
							"\end{tabular*}" _n "\end{table}" _n 
	file close 	t	
		
		
		
		
		
		
		
		
	
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		